home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 58 / pcpp58a.iso / extras / quake 3 source / Q3A_ToolSource.exe / Main / jdapimin.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-02  |  12.5 KB  |  401 lines

  1. /*
  2.  * jdapimin.c
  3.  *
  4.  * Copyright (C) 1994-1995, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains application interface code for the decompression half
  9.  * of the JPEG library.  These are the "minimum" API routines that may be
  10.  * needed in either the normal full-decompression case or the
  11.  * transcoding-only case.
  12.  *
  13.  * Most of the routines intended to be called directly by an application
  14.  * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
  15.  * shared by compression and decompression, and jdtrans.c for the transcoding
  16.  * case.
  17.  */
  18.  
  19. #define JPEG_INTERNALS
  20. #include "jinclude.h"
  21. #include "jpeglib.h"
  22.  
  23.  
  24. /*
  25.  * Initialization of a JPEG decompression object.
  26.  * The error manager must already be set up (in case memory manager fails).
  27.  */
  28.  
  29. GLOBAL void
  30. jpeg_create_decompress (j_decompress_ptr cinfo)
  31. {
  32.   int i;
  33.  
  34.   /* For debugging purposes, zero the whole master structure.
  35.    * But error manager pointer is already there, so save and restore it.
  36.    */
  37.   {
  38.     struct jpeg_error_mgr * err = cinfo->err;
  39.     i = sizeof(struct jpeg_decompress_struct);
  40.     i = SIZEOF(struct jpeg_decompress_struct);
  41.     MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
  42.     cinfo->err = err;
  43.   }
  44.   cinfo->is_decompressor = TRUE;
  45.  
  46.   /* Initialize a memory manager instance for this object */
  47.   jinit_memory_mgr((j_common_ptr) cinfo);
  48.  
  49.   /* Zero out pointers to permanent structures. */
  50.   cinfo->progress = NULL;
  51.   cinfo->src = NULL;
  52.  
  53.   for (i = 0; i < NUM_QUANT_TBLS; i++)
  54.     cinfo->quant_tbl_ptrs[i] = NULL;
  55.  
  56.   for (i = 0; i < NUM_HUFF_TBLS; i++) {
  57.     cinfo->dc_huff_tbl_ptrs[i] = NULL;
  58.     cinfo->ac_huff_tbl_ptrs[i] = NULL;
  59.   }
  60.  
  61.   /* Initialize marker processor so application can override methods
  62.    * for COM, APPn markers before calling jpeg_read_header.
  63.    */
  64.   jinit_marker_reader(cinfo);
  65.  
  66.   /* And initialize the overall input controller. */
  67.   jinit_input_controller(cinfo);
  68.  
  69.   /* OK, I'm ready */
  70.   cinfo->global_state = DSTATE_START;
  71. }
  72.  
  73.  
  74. /*
  75.  * Destruction of a JPEG decompression object
  76.  */
  77.  
  78. GLOBAL void
  79. jpeg_destroy_decompress (j_decompress_ptr cinfo)
  80. {
  81.   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
  82. }
  83.  
  84.  
  85. /*
  86.  * Abort processing of a JPEG decompression operation,
  87.  * but don't destroy the object itself.
  88.  */
  89.  
  90. GLOBAL void
  91. jpeg_abort_decompress (j_decompress_ptr cinfo)
  92. {
  93.   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
  94. }
  95.  
  96.  
  97. /*
  98.  * Install a special processing method for COM or APPn markers.
  99.  */
  100.  
  101. GLOBAL void
  102. jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
  103.                jpeg_marker_parser_method routine)
  104. {
  105.   if (marker_code == JPEG_COM)
  106.     cinfo->marker->process_COM = routine;
  107.   else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
  108.     cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
  109.   else
  110.     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
  111. }
  112.  
  113.  
  114. /*
  115.  * Set default decompression parameters.
  116.  */
  117.  
  118. LOCAL void
  119. default_decompress_parms (j_decompress_ptr cinfo)
  120. {
  121.   /* Guess the input colorspace, and set output colorspace accordingly. */
  122.   /* (Wish JPEG committee had provided a real way to specify this...) */
  123.   /* Note application may override our guesses. */
  124.   switch (cinfo->num_components) {
  125.   case 1:
  126.     cinfo->jpeg_color_space = JCS_GRAYSCALE;
  127.     cinfo->out_color_space = JCS_GRAYSCALE;
  128.     break;
  129.     
  130.   case 3:
  131.     if (cinfo->saw_JFIF_marker) {
  132.       cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
  133.     } else if (cinfo->saw_Adobe_marker) {
  134.       switch (cinfo->Adobe_transform) {
  135.       case 0:
  136.     cinfo->jpeg_color_space = JCS_RGB;
  137.     break;
  138.       case 1:
  139.     cinfo->jpeg_color_space = JCS_YCbCr;
  140.     break;
  141.       default:
  142.     WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  143.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  144.     break;
  145.       }
  146.     } else {
  147.       /* Saw no special markers, try to guess from the component IDs */
  148.       int cid0 = cinfo->comp_info[0].component_id;
  149.       int cid1 = cinfo->comp_info[1].component_id;
  150.       int cid2 = cinfo->comp_info[2].component_id;
  151.  
  152.       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  153.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
  154.       else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
  155.     cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
  156.       else {
  157.     TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
  158.     cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
  159.       }
  160.     }
  161.     /* Always guess RGB is proper output colorspace. */
  162.     cinfo->out_color_space = JCS_RGB;
  163.     break;
  164.     
  165.   case 4:
  166.     if (cinfo->saw_Adobe_marker) {
  167.       switch (cinfo->Adobe_transform) {
  168.       case 0:
  169.     cinfo->jpeg_color_space = JCS_CMYK;
  170.     break;
  171.       case 2:
  172.     cinfo->jpeg_color_space = JCS_YCCK;
  173.     break;
  174.       default:
  175.     WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
  176.     cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
  177.     break;
  178.       }
  179.     } else {
  180.       /* No special markers, assume straight CMYK. */
  181.       cinfo->jpeg_color_space = JCS_CMYK;
  182.     }
  183.     cinfo->out_color_space = JCS_CMYK;
  184.     break;
  185.     
  186.   default:
  187.     cinfo->jpeg_color_space = JCS_UNKNOWN;
  188.     cinfo->out_color_space = JCS_UNKNOWN;
  189.     break;
  190.   }
  191.  
  192.   /* Set defaults for other decompression parameters. */
  193.   cinfo->scale_num = 1;        /* 1:1 scaling */
  194.   cinfo->scale_denom = 1;
  195.   cinfo->output_gamma = 1.0;
  196.   cinfo->buffered_image = FALSE;
  197.   cinfo->raw_data_out = FALSE;
  198.   cinfo->dct_method = JDCT_DEFAULT;
  199.   cinfo->do_fancy_upsampling = TRUE;
  200.   cinfo->do_block_smoothing = TRUE;
  201.   cinfo->quantize_colors = FALSE;
  202.   /* We set these in case application only sets quantize_colors. */
  203.   cinfo->dither_mode = JDITHER_FS;
  204. #ifdef QUANT_2PASS_SUPPORTED
  205.   cinfo->two_pass_quantize = TRUE;
  206. #else
  207.   cinfo->two_pass_quantize = FALSE;
  208. #endif
  209.   cinfo->desired_number_of_colors = 256;
  210.   cinfo->colormap = NULL;
  211.   /* Initialize for no mode change in buffered-image mode. */
  212.   cinfo->enable_1pass_quant = FALSE;
  213.   cinfo->enable_external_quant = FALSE;
  214.   cinfo->enable_2pass_quant = FALSE;
  215. }
  216.  
  217.  
  218. /*
  219.  * Decompression startup: read start of JPEG datastream to see what's there.
  220.  * Need only initialize JPEG object and supply a data source before calling.
  221.  *
  222.  * This routine will read as far as the first SOS marker (ie, actual start of
  223.  * compressed data), and will save all tables and parameters in the JPEG
  224.  * object.  It will also initialize the decompression parameters to default
  225.  * values, and finally return JPEG_HEADER_OK.  On return, the application may
  226.  * adjust the decompression parameters and then call jpeg_start_decompress.
  227.  * (Or, if the application only wanted to determine the image parameters,
  228.  * the data need not be decompressed.  In that case, call jpeg_abort or
  229.  * jpeg_destroy to release any temporary space.)
  230.  * If an abbreviated (tables only) datastream is presented, the routine will
  231.  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
  232.  * re-use the JPEG object to read the abbreviated image datastream(s).
  233.  * It is unnecessary (but OK) to call jpeg_abort in this case.
  234.  * The JPEG_SUSPENDED return code only occurs if the data source module
  235.  * requests suspension of the decompressor.  In this case the application
  236.  * should load more source data and then re-call jpeg_read_header to resume
  237.  * processing.
  238.  * If a non-suspending data source is used and require_image is TRUE, then the
  239.  * return code need not be inspected since only JPEG_HEADER_OK is possible.
  240.  *
  241.  * This routine is now just a front end to jpeg_consume_input, with some
  242.  * extra error checking.
  243.  */
  244.  
  245. GLOBAL int
  246. jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
  247. {
  248.   int retcode;
  249.  
  250.   if (cinfo->global_state != DSTATE_START &&
  251.       cinfo->global_state != DSTATE_INHEADER)
  252.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  253.  
  254.   retcode = jpeg_consume_input(cinfo);
  255.  
  256.   switch (retcode) {
  257.   case JPEG_REACHED_SOS:
  258.     retcode = JPEG_HEADER_OK;
  259.     break;
  260.   case JPEG_REACHED_EOI:
  261.     if (require_image)        /* Complain if application wanted an image */
  262.       ERREXIT(cinfo, JERR_NO_IMAGE);
  263.     /* Reset to start state; it would be safer to require the application to
  264.      * call jpeg_abort, but we can't change it now for compatibility reasons.
  265.      * A side effect is to free any temporary memory (there shouldn't be any).
  266.      */
  267.     jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
  268.     retcode = JPEG_HEADER_TABLES_ONLY;
  269.     break;
  270.   case JPEG_SUSPENDED:
  271.     /* no work */
  272.     break;
  273.   }
  274.  
  275.   return retcode;
  276. }
  277.  
  278.  
  279. /*
  280.  * Consume data in advance of what the decompressor requires.
  281.  * This can be called at any time once the decompressor object has
  282.  * been created and a data source has been set up.
  283.  *
  284.  * This routine is essentially a state machine that handles a couple
  285.  * of critical state-transition actions, namely initial setup and
  286.  * transition from header scanning to ready-for-start_decompress.
  287.  * All the actual input is done via the input controller's consume_input
  288.  * method.
  289.  */
  290.  
  291. GLOBAL int
  292. jpeg_consume_input (j_decompress_ptr cinfo)
  293. {
  294.   int retcode = JPEG_SUSPENDED;
  295.  
  296.   /* NB: every possible DSTATE value should be listed in this switch */
  297.   switch (cinfo->global_state) {
  298.   case DSTATE_START:
  299.     /* Start-of-datastream actions: reset appropriate modules */
  300.     (*cinfo->inputctl->reset_input_controller) (cinfo);
  301.     /* Initialize application's data source module */
  302.     (*cinfo->src->init_source) (cinfo);
  303.     cinfo->global_state = DSTATE_INHEADER;
  304.     /*FALLTHROUGH*/
  305.   case DSTATE_INHEADER:
  306.     retcode = (*cinfo->inputctl->consume_input) (cinfo);
  307.     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
  308.       /* Set up default parameters based on header data */
  309.       default_decompress_parms(cinfo);
  310.       /* Set global state: ready for start_decompress */
  311.       cinfo->global_state = DSTATE_READY;
  312.     }
  313.     break;
  314.   case DSTATE_READY:
  315.     /* Can't advance past first SOS until start_decompress is called */
  316.     retcode = JPEG_REACHED_SOS;
  317.     break;
  318.   case DSTATE_PRELOAD:
  319.   case DSTATE_PRESCAN:
  320.   case DSTATE_SCANNING:
  321.   case DSTATE_RAW_OK:
  322.   case DSTATE_BUFIMAGE:
  323.   case DSTATE_BUFPOST:
  324.   case DSTATE_STOPPING:
  325.     retcode = (*cinfo->inputctl->consume_input) (cinfo);
  326.     break;
  327.   default:
  328.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  329.   }
  330.   return retcode;
  331. }
  332.  
  333.  
  334. /*
  335.  * Have we finished reading the input file?
  336.  */
  337.  
  338. GLOBAL boolean
  339. jpeg_input_complete (j_decompress_ptr cinfo)
  340. {
  341.   /* Check for valid jpeg object */
  342.   if (cinfo->global_state < DSTATE_START ||
  343.       cinfo->global_state > DSTATE_STOPPING)
  344.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  345.   return cinfo->inputctl->eoi_reached;
  346. }
  347.  
  348.  
  349. /*
  350.  * Is there more than one scan?
  351.  */
  352.  
  353. GLOBAL boolean
  354. jpeg_has_multiple_scans (j_decompress_ptr cinfo)
  355. {
  356.   /* Only valid after jpeg_read_header completes */
  357.   if (cinfo->global_state < DSTATE_READY ||
  358.       cinfo->global_state > DSTATE_STOPPING)
  359.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  360.   return cinfo->inputctl->has_multiple_scans;
  361. }
  362.  
  363.  
  364. /*
  365.  * Finish JPEG decompression.
  366.  *
  367.  * This will normally just verify the file trailer and release temp storage.
  368.  *
  369.  * Returns FALSE if suspended.  The return value need be inspected only if
  370.  * a suspending data source is used.
  371.  */
  372.  
  373. GLOBAL boolean
  374. jpeg_finish_decompress (j_decompress_ptr cinfo)
  375. {
  376.   if ((cinfo->global_state == DSTATE_SCANNING ||
  377.        cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
  378.     /* Terminate final pass of non-buffered mode */
  379.     if (cinfo->output_scanline < cinfo->output_height)
  380.       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
  381.     (*cinfo->master->finish_output_pass) (cinfo);
  382.     cinfo->global_state = DSTATE_STOPPING;
  383.   } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
  384.     /* Finishing after a buffered-image operation */
  385.     cinfo->global_state = DSTATE_STOPPING;
  386.   } else if (cinfo->global_state != DSTATE_STOPPING) {
  387.     /* STOPPING = repeat call after a suspension, anything else is error */
  388.     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  389.   }
  390.   /* Read until EOI */
  391.   while (! cinfo->inputctl->eoi_reached) {
  392.     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
  393.       return FALSE;        /* Suspend, come back later */
  394.   }
  395.   /* Do final cleanup */
  396.   (*cinfo->src->term_source) (cinfo);
  397.   /* We can use jpeg_abort to release memory and reset global_state */
  398.   jpeg_abort((j_common_ptr) cinfo);
  399.   return TRUE;
  400. }
  401.